package com.cxc.news.service.Impl;

import com.cxc.news.dao.NewsDao;
import com.cxc.news.pojo.News;
import com.cxc.news.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsDao newsDao;

    @Override
    public News getNews(Long id) {
        return newsDao.getNews(id);
    }

    @Override
    public PageInfo<News> listNews(int pageNum, int pageSize, News news) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> newsList = newsDao.getNewsByCondition(news);
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        return pageInfo;
    }

    @Transactional
    @Override
    public boolean saveNews(News news) {
        news.setCreateTime(new Date());
        news.setUpdateTime(new Date());
        news.setViews(0);
        newsDao.saveNews(news);
        Long id = news.getId();
        return true;
    }

    @Transactional
    @Override
    public boolean updateNews(News news) {
        News b = newsDao.getNews(news.getId());
        news.setUpdateTime(new Date());
        newsDao.updateNews(news);
        return true;
    }

    @Transactional
    @Override
    public void deleteNews(Long id) {
        newsDao.deleteNews(id);
    }

    @Override
    public List<News> getAllNews() {
        return newsDao.getAllNews();
    }

    @Override
    public PageInfo<News> listNewsByStringQuery(int pageNum, int pageSize, String query) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> news = newsDao.getNewsByStringQuery(query);

        PageInfo<News> pageInfo = new PageInfo<>(news);
        return pageInfo;
    }

    @Override
    public List<News> getNewNewsLismitSize(Integer size) {
        return newsDao.getNewsListSize(size);
    }

    @Override
    public Integer getNewsNum() {
        return newsDao.getNewsNum();
    }


    /**
     * 根据分类id分页查询新闻
     * @param pageNum
     * @param pageSize
     * @param typeId
     * @return
     */
    @Override
    public PageInfo<News> listNewsByTypeId(int pageNum, int pageSize, Long typeId) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> newsList = newsDao.getNewsByTypeId(typeId);

        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        return pageInfo;
    }

    @Override
    public List<News> listNewsByTypeIdNoPage(Long typeId) {
        return newsDao.getNewsByTypeId(typeId);
    }

    @Override
    public void addViews(Long id) {
        newsDao.updateViews(id);
    }

    @Override
    public List<News> getTopViewsNews(Integer topNum) {
        return newsDao.getTopViewsNews(topNum);
    }

    @Override
    public List<News> getNewsByUserId(Long userId) {
        return newsDao.getNewsByUserId(userId);

    }

    @Override
    public PageInfo<News> listNewsByUserId(Integer pageNum,Integer pageSize,Long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<News> newsList = newsDao.getNewsByUserId(userId);

        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        return pageInfo;
    }


}
